﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RSALicCore
{
    public static class ImageHelper
    {

        /// <summary>
        /// 将指定的图片，转换成圆形
        /// </summary>
        /// <param name="img"></param>
        /// <returns></returns>
        public static Image CutEllipse(this Image img)
        {
            Rectangle rec = new Rectangle(0, 0, img.Width, img.Height);
            Size size = new Size(img.Width, img.Height);
            Bitmap bitmap = new Bitmap(size.Width, size.Height);
            using (Graphics g = Graphics.FromImage(bitmap))
            {
                using (TextureBrush br = new TextureBrush(img, System.Drawing.Drawing2D.WrapMode.Clamp, rec))
                {
                    br.ScaleTransform(bitmap.Width / (float)rec.Width, bitmap.Height / (float)rec.Height);
                    g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                    g.FillEllipse(br, new Rectangle(Point.Empty, size));
                }
            }
            return bitmap;
        }

        public static Image CutRectangle(this Image img, Rectangle rect)
        {
            Rectangle rec = new Rectangle(0, 0, img.Width, img.Height);
            Size size = new Size(img.Width, img.Height);
            Bitmap bitmap = new Bitmap(rect.Width, rect.Height);
            using (Graphics g = Graphics.FromImage(bitmap))
            {
                using (TextureBrush br = new TextureBrush(img, System.Drawing.Drawing2D.WrapMode.Clamp, rec))
                {
                    br.ScaleTransform(1, 1);
                    br.TranslateTransform(-rect.X, -rect.Y);
                    g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                    g.FillRectangle(br, 0, 0, rect.Width, rect.Height);
                }
            }
            return bitmap;
        }
    }
}
